package com.amsure.utils;

import com.amsure.entity.BaseCommonEntity;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @date 2021/8/21
 * @ddescription:
 */
public class CommonUtils {
    /**
     * @Description: map转string
     * @Param: map
     * @return: string
     */
    public static String getMapToString(HashMap<String, Object> map) {
        Set<String> keySet = map.keySet();
        //将set集合转换为数组
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        //给数组排序(升序)
        Arrays.sort(keyArray);
        //因为String拼接效率会很低的，所以转用StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyArray.length; i++) {
            // 参数值为空，则不参与签名 这个方法trim()是去空格
            if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
                sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
            }
            if (i != keyArray.length - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    /**
     * @Description: string 转 map
     * @Param:
     * @return:
     */
    public static HashMap<String, Object> getStringToMap(String str) {
        //根据逗号截取字符串数组
        String[] str1 = str.split(",");
        //创建Map对象
        HashMap<String, Object> map = new HashMap<>();
        //循环加入map集合
        for (int i = 0; i < str1.length; i++) {
            //根据":"截取字符串数组
            String[] str2 = str1[i].split(":");
            //str2[0]为KEY,str2[1]为值
            //map.put(str2[0],str2[1]);

            if (str2.length == 2) {
                map.put(str2[0].trim(), str2[1]);
            } else {
                map.put(str2[0].trim(), "");
            }
        }
        return map;
    }

    /**
     * @Description: 获取当前时间yyyyMMddHHmmss
     * @Param:
     * @return:
     */
    public static String getTimeNow() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String stringDate = dateFormat.format(new Date());
        return stringDate;
    }

    /**
     * @Description: 随机获得4位数
     * @Param:
     * @return:
     */
    public static String numbersRandomly() {
        Random random = new Random();
        int intNumbersRandomlyFour = random.nextInt(9000) + 1000;
        return Integer.toString(intNumbersRandomlyFour);
    }

    /**
     * @Description: 生成订单编号    格式“运营商ID+yyyyMMddHHmmss+4位随机数”，27字符
     * @Param: operatorId:运营商ID
     * @return: 订单编号
     */
    public static String getTradeNo(String operatorId, String currentTime) {

        return operatorId + currentTime + numbersRandomly();
    }

    /**
     * @Description: 输出结果整理
     * @Param:
     * @return: String informationData
     */
    public static HashMap<String, Object> getCorrespondingValueFromInformation(String informationAll, String informationPartData) {
        String informationAllTemporary = informationAll.substring(1, informationAll.length() - 1);
        String informationPartTemporary = informationPartData.substring(1, informationPartData.length() - 1);

        HashMap<String, Object> stringToMapInformationAll = getStringToMap(informationAllTemporary);
        HashMap<String, Object> stringToMapInformationPart = getStringToMap(informationPartTemporary);

        String succStat = stringToMapInformationPart.get("\"succStat\"").toString();
        stringToMapInformationPart.remove("\"succStat\"");
        stringToMapInformationPart.put("succStat", succStat);

        String failReason = stringToMapInformationPart.get("\"failReason\"").toString();
        stringToMapInformationPart.remove("\"failReason\"");
        stringToMapInformationPart.put("failReason", failReason);

        String tradeNo = stringToMapInformationPart.get("\"tradeNo\"").toString().substring(1, stringToMapInformationPart.get("\"tradeNo\"")
                .toString().length() - 1);
        stringToMapInformationPart.remove("\"tradeNo\"");
        stringToMapInformationPart.put("tradeNo", tradeNo);

        String msg = stringToMapInformationAll.get("\"msg\"").toString().substring(1, stringToMapInformationAll.get("\"msg\"")
                .toString().length() - 1);
        stringToMapInformationAll.remove("\"msg\"");
        stringToMapInformationAll.put("msg", msg);

        stringToMapInformationAll.remove("\"data\"");
        stringToMapInformationAll.put("data", stringToMapInformationPart);

        String ret = stringToMapInformationAll.get("\"ret\"").toString();
        stringToMapInformationAll.remove("\"ret\"");
        stringToMapInformationAll.put("ret", ret);

        String sig = stringToMapInformationAll.get("\"sig\"").toString().substring(1, stringToMapInformationAll.get("\"sig\"")
                .toString().length() - 1);
        stringToMapInformationAll.remove("\"sig\"");
        stringToMapInformationAll.put("sig", sig);

        String operatorId = stringToMapInformationAll.get("\"operatorId\"").toString().substring(1, stringToMapInformationAll.get("\"operatorId\"")
                .toString().length() - 1);
        stringToMapInformationAll.remove("\"operatorId\"");
        stringToMapInformationAll.put("operatorId", operatorId);

        return stringToMapInformationAll;
    }
}
